/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gruppe1projekt.ControllerLayer;

import gruppe1projekt.ModelLayer.EmployeeContainer;
import gruppe1projekt.ModelLayer.Employee;
import gruppe1projekt.ModelLayer.MD5Hash;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 *
 * @author Moulde
 */
public class AuthenticationService
    {

    private static AuthenticationService instance;
    private EmployeeContainer employeeContainer;
    public static Employee User;
    public static Date lastUserAction;

    public static AuthenticationService GetInstance()
    {
        if (instance == null)
        {
            instance = new AuthenticationService();
        }

        return instance;
    }

    private AuthenticationService()
    {
        employeeContainer = EmployeeContainer.GetInstance();
    }

    /** MÅ KUN BRUGES TIL AT TESTE KLASSEN!! */
    public AuthenticationService(ArrayList<Employee> employees)
    {
        System.out.println("WARNING, DENNE CONSTRUCTOR ER KUN TIL AT TESTE KLASSEN \n OG MÅ IKKE BRUGES I SYSTEMET! - Brug altid GetInstance() metoden!");
        employeeContainer = EmployeeContainer.GetInstance();
        employeeContainer.setEmployees(employees);
    }

    private boolean LoginIsValid()
    {
        Date currentDate = new Date();
        Date login = lastUserAction;
        GregorianCalendar CalChecker = new GregorianCalendar();
        CalChecker.setTime(login);
        CalChecker.add(Calendar.MINUTE, 30);
        login = CalChecker.getTime();

        SimpleDateFormat formatter = new SimpleDateFormat();

        if (currentDate.before(CalChecker.getTime()))
        {
            return true;
        }

        return false;
    }

    private boolean IsInRole(String role)
    {
        return User.getRole().trim().equalsIgnoreCase(role.trim());
    }

    public boolean IsAuthenticated(String role)
    {
        return IsAuthenticated() && IsInRole(role);
    }

    public boolean IsAuthenticated()
    {
        if (User != null && LoginIsValid())
        {
            lastUserAction = new Date();
            return true;
        } else
        {
            User = null;
        }

        return false;
    }

    public boolean ValidateLogin(String username, String password)
    {
        if (username.equals("demo") && password.equals("demo"))
        {
            LoginSucceded(new Employee("demo", "demo", "fullname", "address", "Was", "af s", "241saf", "gasg ge"));
            return true;
        }

        for (Employee employee : employeeContainer.getEmployees())
        {
            if (employee.getUsername().equals(username))
            {
                String hashedPassword = HashString(password);
                if (employee.getPasswordHash().equals(hashedPassword))
                {
                    LoginSucceded(employee);
                    return true;
                }
            }
        }

        return false;
    }

    public void LoginSucceded(Employee employee)
    {
        this.User = employee;
        this.lastUserAction = new Date();
    }

    public void Logout()
    {
        this.User = null;
        this.lastUserAction = null;
    }

    public String HashString(String toHash)
    {
        return new MD5Hash(toHash).getResult();
    }
}
